package com.gdpu.controller;

import com.gdpu.api.CommonPage;
import com.gdpu.api.CommonResult;
import com.gdpu.dto.PmsGoodDto;
import com.gdpu.model.PmsGoods;
import com.gdpu.service.PmsGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:weiming
 * @Date:2021/3/16
 * 商品信息模块
 */
@Api(tags = "商品管理")
@RestController
public class PmsGoodController {

    @Autowired
    private PmsGoodsService pmsGoodsService;

    @ApiOperation("获取所有的商品")
    @GetMapping("/goods")
    public CommonResult list(@RequestParam(name = "pageNum",defaultValue = "1")  Integer pageNum,
                                @RequestParam(name = "pageSize",defaultValue = "10")  Integer pageSize){
        CommonPage list = pmsGoodsService.list(pageNum, pageSize);
        return  CommonResult.success(list);
    }

    @ApiOperation("根据商品id获取商品")
    @GetMapping("/good/{id}")
    public CommonResult get(@PathVariable("id") Integer id){
        PmsGoods good = pmsGoodsService.get(id);
        if (good != null){
           return CommonResult.success(good);
        }
        return CommonResult.failed("商品不存在");
    }

    @ApiOperation("添加商品")
    @PostMapping("/good")
    public CommonResult add( @Validated  @RequestBody PmsGoodDto pmsGoodDto){
        int count = pmsGoodsService.add(pmsGoodDto);
        if(count == 1){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }

    @ApiOperation("删除商品")
    @PutMapping("/good/{id}")
    public CommonResult update(@PathVariable("id") Integer id){
        int count = pmsGoodsService.delete(id);
        if(count == 1){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation("更新商品")
    @PutMapping("/good")
    public CommonResult update(@Validated @RequestBody PmsGoodDto pmsGoodDto){
        int count = pmsGoodsService.update(pmsGoodDto);
        if(count == 1){
            return CommonResult.success("商品更新成功！");
        }
        return CommonResult.failed();
    }




}
